package filter;

import entity.User;
import service.UserService;
import utils.CookieUtils;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
@WebFilter("/*")
public class LoginFilter implements Filter {
    private UserService userService = new UserService();
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        request.setCharacterEncoding("UTF-8");

        // 获取请求URI
        String uri = request.getRequestURI();
        // 截取最后一个/后面的访问路径
        String targetName = uri.substring(uri.lastIndexOf("/")+1);
        if (
                uri.startsWith("/bootstrap") ||         //bootstrap 资源路径下的请求全部放行
                        uri.startsWith("/css") ||
                        uri.startsWith("/fonts") ||
                        uri.startsWith("/img") ||
                        uri.startsWith("/js") ||
                        uri.startsWith("/upload") ||
                        uri.startsWith("/vendor") ||


                        targetName.equals("checkCode.jsp") ||       //放行checkCode.jsp页面
                        targetName.equals("forget.jsp") ||
                        targetName.equals("index.jsp") ||
                        targetName.equals("taglib.jsp") ||
                        targetName.equals("register.jsp") ||


                        targetName.equals("login") ||                  // 放行登录请求
                        targetName.equals("register") ||
                        targetName.equals("checkUsername") ||
                        targetName.equals("checkEmail")	||
                        targetName.equals("wx_login")	||

                        targetName.equals("/favicon.ico") 		// 欢迎图标
        ) {

            //放行资源
            filterChain.doFilter(request, response);

            //记住这里要加return,要不然请求放行了代码还是会往下执行
            return;
        }
        //收限资源，需要用户登录
        HttpSession session = request.getSession();
        Object loginUser = session.getAttribute("loginUser");

        if (loginUser != null) {
            //有登录
            filterChain.doFilter(request, response);
            return;
        }

            Cookie userIdCookie = CookieUtils.getCookie(request,"userId");
            if (userIdCookie != null) {
                //虽然是受限资源,但是勾选了记住我，cookie中有用户信息
                Long userId = Long.parseLong(userIdCookie.getValue());
                User user = userService.findById(userId);

                session.setAttribute("loginUser", user);
                //续期cookie7天
                Cookie cookie = new Cookie("userId", user.getId()+"");
                //设置携带路径
                cookie.setPath("/");
                //持久化
                cookie.setMaxAge(60*60*24*7);
                //添加到响应头
                response.addCookie(cookie);

                filterChain.doFilter(request, response);
                return;
            }
            //没登录去登陆
            response.sendRedirect("/index.jsp");
        }

    @Override
    public void destroy() {

    }
}
